import matplotlib.pyplot as plt
import numpy as np 
import math

#设置坐标轴
ax=plt.gca()      
ax.spines["right"].set_color("none")   #设置坐标轴的颜色
ax.spines["top"].set_color("none")
ax.spines["left"].set_position(("data",0))  #设置坐标轴的位置
ax.spines["bottom"].set_position(("data",0))
ax.xaxis.set_ticks_position("bottom")   #x轴的数据显示在x轴右侧
ax.yaxis.set_ticks_position("left")    #y轴的数据显示在y轴左侧
plt.axis([-45,45,-45,45])   #指定绘图范围(x负, x正，y负，y正)
plt.grid()     #显示网格线


X = [0,2,7,3,11]
Y = [0,10,17,31,40]
X = np.array(X)
Y = np.array(Y)
print(X)
print(Y)
plt.plot(0, 0, 'ro')  # 绘制紫红色的圆形的点
plt.plot(X,Y,color='r',linewidth=1.0,linestyle='-')  # 绘制

# X1=X
# Y1=Y
# while(len(X1) > 1):
#     #移动向量
#     if(X1[1]==0):
#         Y1=Y1-Y1[1] #折线向下移动，用减法 （向量移动）
#         #plt.plot(X1,Y1,color='b',linewidth=1.0,linestyle='-')  # 绘制
#         X1 = X1[1:] #截取原点以上的数据
#         Y1 = Y1[1:]
#         print(X1)
#         print(Y1)
#     # 直线在约束在x,y轴上滑动
#     else: 
#         Vx=1    #x轴上滑动速度
#         time_step = 0   #执行次数
#         a=abs(X1[1])  #x轴初始长度
#         b=abs(Y1[1])
#         Ax = a
#         while(Ax > 0):
#             Ax = a - (Vx*time_step)                  # A点在Y轴上长度
#             By = b - ((a**2 + b**2)-Ax**2)**(1/2)    # B点在Y轴上长度
#             print('{}*****{}'.format(Ax, By))
#             if(X1[1] > 0): # 点坐标在 x正轴
#                 x_points = [Ax,0]     #固定x 
#                 y_points = [Y1[1],By] #固定y
#             if(X1[1] < 0): # 点坐标在 x负轴
#                 x_points = [-Ax,0]
#                 y_points = [Y1[1],By]
#             print(x_points)  
#             print(y_points)
#             plt.plot(x_points, y_points) #画线 
#             time_step = time_step + 1
#             plt.pause(1) 
        
#         X1=X1-X1[1] # 矩阵x轴移动
#         print(X1)
#         print(Y1)

#------------------------------------------------------------------

forward_move_y=[] # y轴直线移动值
forward_Ax_points=[] #x轴滑动点集
forward_Ay_points=[] #x轴滑动点集
forward_Bx_points=[] #y轴滑动点集
forward_By_points=[] #y轴滑动点集

X1=X
Y1=Y
while(len(X1) > 1):
    #移动向量
    if(X1[1]==0):
        forward_move_y.append(Y1[1])
        Y1=Y1-Y1[1] #折线向下移动，用减法 （向量移动）
        #plt.plot(X1,Y1,color='b',linewidth=1.0,linestyle='-')  # 绘制
        print('+++++++++++++++++++++++++++++++++')
        print(X1)
        print(Y1)
        print('--------------------------------')        
        X1 = X1[1:] #截取原点以上的数据
        Y1 = Y1[1:]
        print(X1)
        print(Y1)
        print('+++++++++++++++++++++++++++++++++')
    # 直线在约束在x,y轴上滑动
    else: 
        Vx=0.5    #x轴上滑动速度
        time_step = 0   #执行次数
        a=abs(X1[1])    #x轴初始长度
        b=abs(Y1[1])
        Ax = a
        while(Ax > 0):
            Ax = a - (Vx*time_step)                  # A点在Y轴上长度
            By = b - ((a**2 + b**2)-Ax**2)**(1/2)    # B点在Y轴上长度
            print('{}*****{}'.format(Ax, By))
            if(X1[1] > 0): # 点坐标在 x正轴
                x_points = [Ax,0]     #固定x 
                y_points = [Y1[1],By] #固定y
            if(X1[1] < 0): # 点坐标在 x负轴
                x_points = [-Ax,0]
                y_points = [Y1[1],By]
            print(x_points)  
            print(y_points)
            forward_Ax_points.append(x_points[0])
            forward_Ay_points.append(y_points[0])
            forward_Bx_points.append(x_points[1])
            forward_By_points.append(y_points[1])
            plt.plot(x_points, y_points, color='g',linewidth=1.0,linestyle='--') #画线 
            time_step = time_step + 1
            plt.pause(0.1) 
        
        X1=X1-X1[1] # 向量在x轴移动，使其变为0
        print(X1)
        print(Y1)

print(X)
print(Y)
print(forward_move_y)
print(forward_Ax_points)
print(forward_Ay_points)
print(forward_Bx_points)
print(forward_By_points)

plt.show()
#---------------------数据处理----------------------------------------------
#查询某个元素的索引
#print(forward_Ay_points.index(10)) #只返回第一个元素
#print([i for i, x in enumerate(forward_Ay_points) if x==10])

Ay_i_list = []
for i in forward_move_y:
    Ay_i = forward_Ay_points.index(i)
    Ay_i_list.append(Ay_i)
print(Ay_i_list)

for i in range(0,len(Ay_i_list)):
    print(i)
    if(i == len(Ay_i_list)-1):
        forward_Ax_new_1 = forward_Ax_points[Ay_i_list[i]:]
        forward_Ay_new_1 = forward_Ay_points[Ay_i_list[i]:]
        forward_Bx_new_1 = forward_Bx_points[Ay_i_list[i]:]
        forward_By_new_1 = forward_By_points[Ay_i_list[i]:]
    else:
        forward_Ax_new_1 = forward_Ax_points[Ay_i_list[i]:Ay_i_list[i+1]]
        forward_Ay_new_1 = forward_Ay_points[Ay_i_list[i]:Ay_i_list[i+1]]
        forward_Bx_new_1 = forward_Bx_points[Ay_i_list[i]:Ay_i_list[i+1]]
        forward_By_new_1 = forward_By_points[Ay_i_list[i]:Ay_i_list[i+1]]

    print(forward_Ax_new_1)
    print(forward_Ay_new_1)
    print(forward_Bx_new_1)
    print(forward_By_new_1)




